﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;

namespace Holidays
{
    public partial class Places : PhoneApplicationPage
    {
        string holId = "";
        save s = new save();
        public Places()
        {
            InitializeComponent();
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            if (NavigationContext.QueryString.TryGetValue("holId", out holId))
                listplaces.ItemsSource = s.LoadPlaces();
        }


        private void AppBarSaveButton_Click(object sender, EventArgs e)
        {
            IEnumerable<DependencyObject> cboxes = Static.GetChildsRecursive(listplaces);
            List<string> strlist = new List<string>();
            int count = 1;
            string aplace = string.Empty;
            foreach (DependencyObject obj in cboxes.OfType<CheckBox>())
            {
                Type type = obj.GetType();
                if (type.Name == "CheckBox")
                {
                    CheckBox cb = obj as CheckBox;
                    if (cb.IsChecked == true)
                        strlist.Add(cb.Tag.ToString());
                }
            }
            foreach (string str in strlist)
            {

                if (count == 1)
                {
                    aplace = str;

                    count++;
                }
                else
                {
                    aplace += "/" + str;


                }
            }

            s.SaveHolidayPlan(holId, aplace);
            NavigationService.Navigate(new Uri("/Views/Details.xaml", UriKind.Relative));
        }
    }
}